// pages/fileprint/print/newsetting.js
var app=getApp()
Page({

  
  data: {  
    // 显示设置规格
    filemodal:false,
    // 份数
    number:1,
    // 显示余额和微信支付的弹窗
    ispay:true,
    animation:3,
    // 余额微信参数
    radio1:1,
    // 支付弹窗动画参数
    animationpay:1,
    // 打印尺寸参数
    arr5:['拉伸全图','自适应','自定义'],index5:0,
     // 对齐方式参数
     arr2:['左上','左中','左下','中上','中','中下','右上','右中','右下'],index2:4,
    //  缩放比例
     number1:1,
    //  打印范围选中参数
    type:3,
    page:2,
    value1:1,
    value2:2,
    // 配送方式选中的参数
    radio:1,
    address:'',
    // 预约打印的参数
    radio7:'',
    // 上个页面传过来的总价
    payall:'',
    // 余额支付的参数
    passwordvalue:'',value5:'',last:''
   

  },

  onLoad: function (options) {
    if(options.filename){
      this.setData({
           filename:options.filename
      })
    }
    
    
    
    this.getparams(options.id)
      this.setData({
        payall:Number(options.payall).toFixed(2),
        code:options.code
      })
      this.setData({
        specname:app.globalData.specname,
        page:app.globalData.page,
        sheets:app.globalData.sheets,
        number:app.globalData.number,
      })
  },


  onShow: function () {
    this.getaddress()
    this.getyuemoney()
  
  },
//   关闭显示设置规格页面的弹窗
  close(){
    this.setData({
      animation:2
    })
  setTimeout(() => {
    this.setData({
      filemodal:false
    })
  }, 1000);
  },
  // 打开显示设置规格页面的弹窗
  showsetting(){
     this.setData({
      filemodal:true,
      animation:1
     })
  },
   // 打开显示支付页面的弹窗
   showpay(){
     this.setData({
       animationpay:1,
       animation:2
     })
     setTimeout(() => {
      this.setData({
        ispay:true,
        filemodal:false
     })
     }, 1000);
     
   },
    // 关闭显示支付页面的弹窗
   closepay(){
     this.setData({
      animationpay:2,
      
     })
     setTimeout(() => {
      this.setData({
       ispay:false,
        
       })
     }, 1000);
   },
  //  打印尺寸
   bindPickerChange5(e){
    console.log(e);
    
    this.setData({index5:e.detail.value})
    if(e.detail.value==0){
      this.setData({
         radio5:3
      })
    }
    if(e.detail.value==1){
      this.setData({
         radio5:4
      })
    }
    if(e.detail.value==2){
      this.setData({
         radio5:6
      })
    }
  },
  //  对齐方式
  bindPickerChange2(e){
    console.log(e);
    this.setData({
      index2:e.detail.value
    })
    
  },
  jianclick1(){
    let num=this.data.number1
    if(num==1){
      return
    }
    num--
    this.setData({number1:num})
 },
 addclick1(){
   let num=this.data.number1
   if(num==100){
     return
   }
   num++
   this.setData({number1:num})
 },
 changeinput(e){
  console.log(e);
  
    console.log(typeof(e.detail.value));
    let abc=Number(e.detail.value)
    console.log(typeof(abc),abc);
    
    
        this.setData({
           number1:abc
        })  
},
jianclick(){
  console.log(1);
  
  let value=this.data.number
  
  if(value>1){
    value--
    this.setData({
      number:value
    })
    
  }
  let price=this.data. price*this.data.number*(this.data.sheets?this.data.sheets:1)+(this.data.radio==2?1:0)
  if(price<this.data.man){
     this.setData({
       jian:0
     })
  }
},
addclick(){
  let value=this.data.number
  value++
  this.setData({
    number:value
  })
  let price=this.data. price*this.data.number*(this.data.sheets?this.data.sheets:1)+(this.data.radio==2?1:0)
  if(price<this.data.man){
     this.setData({
       jian:0
     })
  }
},
toggle4(e){
  this.setData({
    taocanindex:e.currentTarget.dataset.taocanindex
  })
},
toggle13(e){
  this.setData({
    type:e.currentTarget.dataset.type
  })
  let sheets
  if(this.data.page==''||this.data.page==1){sheets=1}else if(this.data.type==1){
    if(this.data.page%2==0){
      sheets=this.data.page/2
      console.log(1111);
      
    }else{
     sheets=parseInt(this.data.page/2)+1
     console.log(222);
     
    }
  }else if(this.data.type==2){
       
         sheets=parseInt(this.data.page/2)
         console.log(333);
         
       

  }else{sheets=this.data.value2-this.data.value1+1}
  this.setData({sheets})
  let price=this.data. price*this.data.number*(this.data.sheets?this.data.sheets:1)+(this.data.radio==2?1:0)
  if(price<this.data.man){
     this.setData({
       jian:0
     })
  }
  /*this.setData({basesheets:sheets})
  let arr=this.data.speclist[1].data
  var title
 arr.forEach((item)=>{
    if(item.checked==true){
      title=item.title
    }
 })
  console.log(arr,title);
  
  
  console.log(title);
  
  if(title=="长边"||title=="短边"){
    this.setData({flag:2})
    if(sheets%2==0){
       sheets=sheets/2
    }else{
      sheets=parseInt(sheets/2)+1
    }
    this.setData({sheets})
  }else{
    this.setData({sheets,flag:1})
  }*/
 
  
},
toggle(e){
       
  let radio=e.currentTarget.dataset.radio
  this.setData({
    radio
  })
 
    // this.getaddress()
  
    
},
  //获取用户地址
  getaddress(){
    wx.request({
      url: 'https://zujieba.com/Api/car/address.html',
      data:{uid:wx.getStorageSync('uid')},
      method:'POST',
      success:(res)=>{
        console.log(res);
        if(res.data.success==1){
        let address=res.data.data.province+res.data.data.city+res.data.data.area+res.data.data.address
        this.setData({address,address_name:res.data.data.name,address_phone:res.data.data.phone,address_id:res.data.data.id})}
        else{return}
        
      }
    })
  },
  //跳转到我的地址
  navaddress(){
    wx.navigateTo({
      url: '/pages/mine/address/address',
    })
  },
  
   payclick1(){
    this.setData({
      onshow:false
    })
    
   
   this.throttle(()=>{

    let priceid=''
    if(this.data.radio1==3){
       priceid=this.data.taocan[this.data.taocanindex].id
    }
     let params=this.data.paramobj
     params.pay=this.data.radio1
     params.priceid=priceid
     console.log(params);
     
     
     var that =this
     wx.request({
       url: 'https://zujieba.com/Api/order/new_add_order.html',
       data:params,
       method:'POST',
       success:(res)=>{
         console.log(res);
         if(res.data.success==1){
         that.setData({
           code:res.data.data
         })
         console.log(res.data.data);
         
      
        wx.request({
          url: 'https://zujieba.com/Api/order/pay_order.html',
          data:{code:res.data.data},
          success:(res)=>{
            if(that.data.radio1==1){
              console.log(1111,res);
              if(res.success!='2'){
               that.setData({
                 appId:res.data.appId,
                 nonceStr:res.data.nonceStr,
                 package:res.data.package,
                 paySign:res.data.paySign,
                 signType:res.data.signType,
                 timeStamp:res.data.timeStamp,
                 
               })
               that.getpay()
              }else{wx.showToast({
                title: "失败",
                mask:true
              })}
           
            }else if(that.data.radio1==2){
              let price=Number(that.data.payall)
              if(price>Number(that.data.money)){
                 return  wx.showToast({
                   title: '余额不足',
                   icon:'none',
                   mask:true
                 })
              }
              that.getmima()
             
            }else{
             let price=Number(that.data.payall)
             if(price>Number(that.data.taocan[that.data.taocanindex].price)){
                return  wx.showToast({
                  title: '余额不足',
                  mask:true
                })
             }
             that.getmima()
            }
            
           
            
          }
        })
        
         
       }else{
         wx.showToast({
           title: res.data.tips,
           icon:'none',
           mask:true
         })
       }
      
      }
      
  
     })
   },1500)

   },
     //支付
     getpay(){
      wx.requestPayment(
        {
        'timeStamp': this.data.timeStamp,
        'nonceStr': this.data.nonceStr,
        'package': this.data.package,
        'signType': this.data.signType,
        'paySign': this.data.paySign,
        'success':(res)=>{
        let url=''
          console.log(res);
          
                wx.showToast({
                  title: '支付成功',
                  icon:'success',
                  duration:2000,
                  mask:true,
                  
                })
                if(this.data.flag==17){
                           url=this.data.url
                }
               if(this.data.paramobj.now==1||this.data.paramobj.now==3){
  
                 this.getxunhuan2()
                
               }else{
                 wx.hideLoading({
                   complete: (res) => {},
                 })
                wx.navigateTo({
                  url: '/pages/canprint/canprint?code='+this.data.code+"&url="+url,
                })
               }
  
               
               
           
               
          
        },
        fail:(res)=>{
              wx.navigateTo({
                url: '/pages/orderprint/orderprint?change=1'
              })
        }
        
        })
    },
 //获取用户是否设置过密码
 getmima(){
  wx.request({
    url: 'https://zujieba.com/Api/order/pay_pass.html',
    method:"POST",
    data:{uid:wx.getStorageSync('uid')},
    success:(res)=>{
      console.log(res);
      if(res.data.success==1){
        this.setData({show:true})
      }else{
        this.setData({show1:true})
      }
      
    }
  })
},
 //获取用户的余额
 getyuemoney(){
  wx.request({
    url: 'https://zujieba.com/Api/member/member.html',
    data:{uid:wx.getStorageSync('uid')},
    method:'POST',
    success:(res)=>{
      console.log(res);
      console.log(typeof(res.data.data.price),typeof(this.data.payall))
      this.setData({
         money:res.data.data.price,
         radio1:1
      })
      // if(parseFloat(res.data.data.price)>0&&this.data.typeobj.package==1){
      //   this.setData({radio1:2})
      //   console.log(parseFloat(res.data.data.price));
        
        
        
      // }else{
      //   this.setData({radio1:1})

      // }
      
    }
  })
},
toggle1(e){
  console.log(e);
  
  let radio1=e.currentTarget.dataset.radio1
  
  this.setData({
    radio1,
    icon:false
   
  })
},
navpaypass(){
  this.setData({show1:false})
  wx.navigateTo({
    url: '/pages/mine/paypass/paypass?phone='+wx.getStorageSync('phone'),
  })
},
shanchu(){
this.setData({
  show1:false
})
},
onHide(){
this.setData({show:false,show1:false})
},
close2(){
  this.setData({
    show:false
  })
  wx.navigateTo({
    url: '/pages/orderprint/orderprint?change=1',
  })
},
  sureclick(){
    wx.showLoading({
      title: '正在打印...',
      mask:true
    })
    console.log(this.data.near_id,this.data.goods_id);
    
      wx.request({
        url: 'https://zujieba.com/Api/order/pay_order.html',
        data:{paypass:this.data.value5,code:this.data.code},
        method:'POST',
        success:(res)=>{
          console.log(res);
          
          if(res.data.success==1){
            this.setData({
              show:false
            })
            
            let url=''
            if(this.data.flag==17){
              url=this.data.url
            }
          console.log(res);
           if(this.data.paramobj.now==1||this.data.paramobj.now==3){
            wx.request({
              url: 'https://zujieba.com/api/Test2/print.html',
              data:{type:1,status:1,code:this.data.code},
              success:(res1)=>{
                console.log(this.data.code,this.data.package);
                
                if(res1.data.success==1){
                  console.log(res1);
                  
                  wx.request({
                    url: 'https://zujieba.com/api/Test2/print.html',
                    data:{type:2,status:1,code:this.data.code},
                    success:(res)=>{
                      console.log(res);
                      
                      wx.hideLoading()
                      if(res.data.success==1){
                        
                      let task_id=res.data.data.data.task_id
                      let deviceId=res.data.data.order.deviceId
                      let deviceKey=res.data.data.order.deviceKey
                      this.getxunhuan(task_id,deviceId,deviceKey)
                    
                    
                    }else{wx.showToast({
                         title: res.data.tips,
                         icon:'none',
                         mask:true
                       })
                      console.log(res1);
                      }
                    }})
                  
                }else{
                  wx.hideLoading(
                         
                    )
                  wx.showToast({
                    title: res1.data.tips,
                    mask:true
                  })
                }
                
              }
            })
           }else{
             wx.hideLoading({
               complete: (res) => {},
             })
            wx.navigateTo({
              url: '/pages/canprint/canprint?code='+this.data.code+"&url="+url,
            })
           }
           
           
              this.setData({
                near_id:'',
                goods_id:''
              })
              this.setData({value5:'',passwordvalue:[]})
              
          }else if(res.data.success==2){
            console.log(res,this);
            wx.showToast({
              title: res.data.tips,
              icon:'none',
              mask:true
            })
            
           
            this.setData({value5:'',passwordvalue:[]})
            console.log(this.data.value5);
            
          }else{
            this.setData({value5:'',passwordvalue:[],show:false,show1:true})
          }
         
          
        },
        fail:(res)=>{
          console.log(res);
          
          wx.showToast({
            title: res.data.tips,
            duration:2000,
            mask:true
          })
          this.setData({value5:''})
        }
      })
  },
  getxunhuan(task_id){
    setTimeout(()=>{
     wx.request({
       url: 'https://zujieba.com/api/Test2/print.html',
       
       data:{task_id,type:3,status:1,code:this.data.code},
       success:(res2)=>{
          console.log(res2);
          
         
         var title
         if(res2.data.data.data.task_state=='READY'){
            title='排队中'
         }else if(res2.data.data.data.task_state=='PARSING'){
            title='解析中'
         }else if(res2.data.data.data.task_state=='SENDING'){
           title='发送中'
         }else if(res2.data.data.data.task_state=='SUCCESS'){
           title='成功'
         }else if(res2.data.data.data.task_state=='FAILURE'){
           title='失败'
         }
         wx.showLoading({
           title: title,
           mask:true
         })
        
         if(res2.data.data.data.task_state=='SUCCESS'){
           this.setData({flag:true})
           
             wx.hideLoading()
             wx.navigateTo({
               url: '/pages/orderprint/orderprint?change=3',
             })
          
          
         }else if(res2.data.data.data.task_state=='FAILUER'){
           wx.hideLoading()
           this.setData({flag:false})
         }else{
           this.getxunhuan(task_id)
         }
        
       }
     })
    },1000)
 },
 input(e){
   console.log(e);
   
  let str
  if(e.detail.value.length>6){
   
 let value5= e.detail.value.substring(0,6) 
 this.setData({value5})
  }else{
    str=e.detail.value
    this.setData({value5:str})
  }
  console.log(str)
  
  
  let arr=  str.split('')

  console.log(arr);
  
    this.setData({
      passwordvalue:arr
    })
},
// 获取参数
 getparams(id){
   wx.request({
     url: 'https://zujieba.com/api/car/show_car.html',
     data:{id},
     method:'POST',
     success:(res)=>{
       console.log(res);
       let paramobj=this.data.paramobj

       this.gettaocan(res.data.data.goods_id)
        this.setData({
          ['paramobj']:res.data.data
        })
        console.log(this.data.paramobj);
        
     }
   })
 },
 //节流
 throttle:function(func,wait){
  console.log(func,wait);
  
  let last =this.data.last
  console.log(last);
  
  let current_time= +new Date()
  if(current_time-last>wait){
    console.log(1111);
      console.log(current_time,current_time-last);
      
      func()
      last=+new Date()
      this.setData({
        last
      })
  }
    
     
  
   
},
 // 获取店铺余额的套餐
 gettaocan(goods_id){
  wx.request({
    url: 'https://zujieba.com/Api/store/member_price',
    data:{goods_id,uid:wx.getStorageSync('uid')},
    method:'POST',
    success:(res)=>{
      console.log(res);
      this.setData({
        taocan:res.data.data.data,
        msg:res.data.data.msg
      })
    }
  })
},
toggle2(e){
  console.log(e);
  
   let icon=this.data.icon
   this.setData({
     icon:!icon,
     
     radio1:e.currentTarget.dataset.radio1
   })
},
getxunhuan2(){
  setTimeout(()=>{
    wx.request({
      url: 'https://zujieba.com/Api/Test2/test_print',
      data:{code:this.data.code},
      method:'POST',
      success:(res2)=>{
        console.log(res2);
        var title
      if(res2.data.data.task_state=='READY'){
         title='排队中'
      }else if(res2.data.data.task_state=='PARSING'){
         title='解析中'
      }else if(res2.data.data.task_state=='SENDING'){
        title='发送中'
      }else if(res2.data.data.task_state=='SUCCESS'){
        title='成功'
      }else if(res2.data.data.task_state=='FAILURE'){
        title='失败'
      }
      wx.showLoading({
        title: title,
        mask:true
      })
      console.log(res2);
      if(res2.data.data.task_state=='SUCCESS'){
        wx.hideLoading()
        wx.navigateTo({
          url: '/pages/orderprint/orderprint?change=3',
        })
       
       
       
      }else if(res2.data.data.task_state=='FAILUER'){
        wx.hideLoading()
       
      }else{
        this.getxunhuan2()
      }


      }
    })
  },1000)
},
})